<?php

use Biz\model\package\PackageLogistics;
use Biz\service\package\PackageOrder;
use Doraemon\model\Article;
use Doraemon\model\DecorationTask\UserTaskDiary;
use Doraemon\model\Diary;
use Doraemon\service\statSign\Basic as statSign;
use Doraemon\service\User;
use Biz\service\trade\UserAddress;
use Doraemon\tools\Hhzurl;

class DetailAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const SIGN_1 = 1;
    public function main()
    {
        $sign = $this->getRequest()->getQuery('sign', 1); //是否只需要是否购买信息 0, 1
        $uid = $this->uid;
        if ($uid) {
            $userInfo = User::getUser($uid);
            $data['user']['nick'] = $userInfo['nick'];
            $data['user']['avatar'] = $userInfo['avatar'];
            $data['user']['type'] = $userInfo['type'];
            // 是否购买
            $order = PackageOrder::getPayOrder($uid);
            if ($order) {
                $data['buy'] = 1;
                $data['user']['pay_time'] = $order['pay_time'];
            } else {
                $data['buy'] = 0;
                $data['user']['pay_time'] = 0;
            }
        } else {
            $data['qa'] = [
                ['q' => '没有登录', 'a' => 'aaaaaaaaa'],
                ['q' => '没有登陆', 'a' => 'aaaaaaaaa2'],
            ];
            $order = null;
        }

        if ($sign == self::SIGN_1) {
            $this->setSuccess($data);
        }
        // 礼包使用体验部分 预埋6-8篇内容 每次进入页面随机展示4篇
        $data['feedback'] = [
            ['username' => 'cyz', 'avatar' => '', 'content' => '礼包使用体验部分 预埋6-8篇内容 每次进入页面随机展示4篇', 'package_id' => 1]
        ];

        // 有没有发布整屋或者日记
        $data['has_article'] = 0;
        $data['has_diary'] = 0;
        $data['package_id'] = 0;

        if ($order) {
            $data['package_id'] = $order['package_id'];
            $hasArticle = Article::get('aid', [
                'uid' => $uid,
                'addtime[>]' => $order['pay_time'],
                'status' => [Article::STATUS_WAIT_CHECK, Article::STATUS_SUCCESS, Article::STATUS_PRIVATE]
            ]);
            if ($hasArticle) {
                $data['has_article'] = 1;
            }
            $hasDiary = UserTaskDiary::get('id', ['uid' => $uid, 'diary_status' => UserTaskDiary::STATUS_NORMAL, 'addtime[>]' => date("Y-m-d H:i:s", $order['pay_time'])]);
            if ($hasDiary) {
                $data['has_diary'] = 1;
            }
            $data['order_no'] = $order['order_no'];
            // 是否有收货信息
            if ($order['address_id']) {
                $data['address'] = 1;
            } else {
                $data['address'] = 0;
            }
            // 查询物流状态
            $logistics = PackageLogistics::selectByWhere(['order_no' => $order['order_no']], ['express_company', 'order_logistics_no', 'status', 'goods_id']);
            $logisticsGoods = array_column($logistics, null, 'goods_id');

            if (isset($logisticsGoods[PackageOrder::PACKAGE_GOOD_1])) {
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['status_name'] = PackageLogistics::STATUS_MAP[$logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['status']];
            } else {
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['status_name'] = '待发货';
            }
            if (isset($logisticsGoods[PackageOrder::PACKAGE_GOOD_2])) {
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['status_name'] = PackageLogistics::STATUS_MAP[$logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['status']];
            } else {
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['status_name'] = '待发货';
                // 判断是否发过日记
                if (!$hasDiary) {
                    $logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['status_name'] = '去发布日记';
                }
            }
            if (isset($logisticsGoods[PackageOrder::PACKAGE_GOOD_3])) {
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['status_name'] = PackageLogistics::STATUS_MAP[$logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['status']];
            } else {
                $logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['status_name'] = '待发货';
                // 判断是否发过整屋
                if (!$hasArticle) {
                    $logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['status_name'] = '去发布整屋';
                }
            }
            // 拼link, 判断有没有默认地址
            $address = UserAddress::getDefaultAddress($this->uid);
            // 发货地址
            if (empty($address)) {
                $link1 = $link2 = $link3 = \Biz\tools\helper\Common::formatFullScreenLink(Hhzurl::h5PackageShipping($order['order_no'])); // 提交地址页面
            } else {
                $link1 = \Biz\tools\helper\Common::formatFullScreenLink(Hhzurl::h5PackageLogistics($order['order_no'], 1)); // 查看物流页面
                $link2 = \Biz\tools\helper\Common::formatFullScreenLink(Hhzurl::h5PackageLogistics($order['order_no'], 2)); // 查看物流页面
                $link3 = \Biz\tools\helper\Common::formatFullScreenLink(Hhzurl::h5PackageLogistics($order['order_no'], 3)); // 查看物流页面
                if (!$hasDiary) {
                    $link2 = 'niceliving://editor/create?protego=eyJ0eXBlIjoiZGlhcnkiLCJjb250ZW50IjoiIn0%3D';
                }
                if (!$hasArticle) {
                    $link3 = 'niceliving://editor/create?protego=eyJ0eXBlIjoiYXJ0aWNlbCIsImNvbnRlbnQiOiIifQ%3D%3D';
                }
            }
            $h5PackageAssistant = \Biz\tools\helper\Common::formatFullScreenLink(Hhzurl::h5PackageAssistant()); // 需求分析助手

            // 礼包状态
            if ($order['package_id'] == PackageOrder::PACKAGE_ID_1) {
                $data['package'] = [
                    ['title' => '需求分析助手', 'status' => '去使用', 'link' => $h5PackageAssistant, 'order_no' => ''],
                    ['title' => "新装100问\n装修告知书\n开工御守", 'status' => $logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['status_name'], 'link' => $link1, 'order_no' => $order['order_no']],
                ];
                // Q&A
                $data['qa'] = [
                    ['q' => '基础包ssssssssssssssssssssssssssssss', 'a' => 'aaaaaaaaa'],
                    ['q' => '基础包ssssssssssssssssssssssssssssss', 'a' => 'aaaaaaaaa2'],
                ];
            } else {
                $data['package'] = [
                    ['title' => '需求分析助手', 'status' => '去使用', 'link' => $h5PackageAssistant, 'order_logistics_no' => ''],
                    ['title' => "新装100问\n装修告知书\n开工御守+资料夹+冰箱贴*1", 'status' => $logisticsGoods[PackageOrder::PACKAGE_GOOD_1]['status_name'], 'link' => $link1, 'order_no' => $order['order_no']],
                    ['title' => '配套冰箱贴', 'status' => $logisticsGoods[PackageOrder::PACKAGE_GOOD_2]['status_name'], 'link' => $link2, 'order_no' => $order['order_no']],
                    ['title' => '入户门垫', 'status' => $logisticsGoods[PackageOrder::PACKAGE_GOOD_3]['status_name'], 'link' => $link3, 'order_no' => $order['order_no']],
                ];
                $data['qa'] = [
                    ['q' => '高级包ssssssssssssssssssssssssssssss', 'a' => 'aaaaaaaaa'],
                    ['q' => '高级包ssssssssssssssssssssssssssssss', 'a' => 'aaaaaaaaa2'],
                ];
            }
        }

        // 权益解读
        $rights = json_decode(PackageOrder::PACKAGE_RIGHT, true);
        foreach ($rights as $key => $right) {
            $rights[$key]['statSign'] = statSign::genStatSign(statSign::MODULE_NOTE_DETAILS_RELATED_ARTICLE, [
                statSign::EXTEND_KEY_RELATE_OBJ_ID => $right['id'],
            ]);
        }
        $data['rights'] = $rights;
        $this->setSuccess($data, self::API_CODE_SUCCES);
    }
}